<?php
/**
 * 用户相关模型
 * @author jing
 *
 */
class user extends Model {
	var $_table = "xdf_user";
	var $_field = array('id','role_id','create_user_id','name','phone','mail','salt','password','sentence','icon','qq','create_time');
	var $_type_table = "xdf_plant";
	var $_type_field = array('id','user_id','token','type','clientid','devicetoken');
	var $_icon_table = "xdf_image";
	
	/**
	 * 添加用户的手机设备信息
	 *更新用户的设备为最后一次登陆的设备信息
	 * @param unknown_type $userid
	 * @param unknown_type $osType
	 * @param unknown_type $token
	 * @return boolean resource
	 */
	public function setPhoneInfo($userid, $osType = NULL, $token = NULL,$client = NULL,$device = NULL) {
		$sql_row = "SELECT * FROM $this->_type_table WHERE user_id = $userid";
		$row = $this->fetchRow($sql_row);
		if ($row){
			$sql = "UPDATE $this->_type_table SET type = '" . $osType . "',token = '" . $token . "',clientid = '".$client."',devicetoken = '".$device."' WHERE user_id =" . $userid . " ";
			return $this->update ( $sql );
		}else {
			$sql = "INSERT INTO $this->_type_table VALUES(NULL,'$userid','$token','$osType','$client','$device',NOW())";
			return $this->insert ( $sql );
		}
	}
	
	/**
	 * 非正常情况退出时的注销
	 */
	public function setPhoneInfoClear($client, $osType = NULL, $device = NULL, $token = NULL) {
		$sql_row = "SELECT * FROM $this->_type_table WHERE clientid = '".$client."' AND type = '".$osType."'";
		$row = $this->fetchRow($sql_row);
		if ($row){
			$sql = "UPDATE $this->_type_table SET token = '" . $token . "',clientid = '".NULL."',devicetoken = '".$device."' WHERE clientid = '".$client."'   AND type = '".$osType."'";
			return $this->update ( $sql );
		}
	}
	
	/**
	 * 获取设备信息
	 */
	public function getPhoneInfo($userid){
		$sql_row = "SELECT * FROM $this->_type_table WHERE user_id = $userid";
		return $this->fetchRow($sql_row);
	}
	
	/**
	 * 获取用户的头像
	 */
	public function getUserIcon($id){
		$sql_row = "SELECT * FROM $this->_icon_table WHERE id = $id";
		return $this->fetchRow($sql_row);
	}
	/**
	 * 获取用户信息
	 */
	public function getUserInfo($userid){
		$sql_row = "SELECT * FROM $this->_table WHERE id = $userid";
		return $this->fetchRow($sql_row);
	}
	
	/**
	 * 验证用户名密码
	 *
	 * @param unknown_type $username        	
	 * @param unknown_type $password        	
	 * @return array( 'result', 'userinfo', 'msg') | boolean
	 *         result:成功：id 密码错误：-1 用户不存在：-2
	 *         userinfo：用户信息
	 *         msg：描述
	 */
	public function authUser($user, $password = NULL) {
		$result = array ();
		$userinfo = $this->getUserByPhone ( $user );
		if ($userinfo) {
// 			var_dump($userinfo ['password']);
// 			var_dump($this->generatePw ( $password, $userinfo ['salt']));
			if ($userinfo ['password'] == $this->generatePw ( $password, $userinfo ['salt'])) {
				$result ['result'] = $userinfo ['id'];
				$result ['userinfo'] = $userinfo;
				$result ['msg'] = "成功";
			} elseif (empty($userinfo ['password'])) {
				$result ['result'] = - 3;
				$result ['userinfo'] = null;
				$result ['msg'] = "首次登陆请设置密码";
			}else {
				$result ['result'] = - 1;
				$result ['userinfo'] = null;
				$result ['msg'] = "密码错误";
			}
		} else {
			$result ['result'] = - 2;
			$result ['userinfo'] = null;
			$result ['msg'] = "用户不存在";
		}
		return $result;
	}
	/**
	 * 根据用户id获取用户信息
	 *
	 * @param int $userid        	
	 * @return Ambigous <boolean, multitype:>
	 */
	public function getInfoByid($userid) {
		$fields = implode(',',$this->_field);
		$sql =  "SELECT $fields FROM $this->_table WHERE id = $userid";
		return $this->fetchRow($sql);
	}
	/**
	 * 根据用户手机号获取用户信息
	 * return false / array()
	 */
	public function getUserByPhone($val) {
		$fields = implode(',',$this->_field);
		$sql ="SELECT $fields FROM $this->_table WHERE phone = '$val'";
		return $this->fetchRow ( $sql );
	}
	
	
	/**
	 *修改密码
	 *@author jing
	 * @param unknown_type $uid        	
	 * @param unknown_type $pw        	
	 * @return string boolean
	 */
	public function setPw($uid, $pw) {
		$salt = $this->_getSalt ( $uid );
		$password = $this->generatePw ( $pw, $salt );
		$sql = "UPDATE $this->_table SET password = '$password',salt = '$salt' WHERE id =" . $uid . " ";
		return $this->update ( $sql );
	}
	
	/**
	 * 生成加密后的密码
	 *
	 * @param unknown_type $pw        	
	 * @param unknown_type $salt        	
	 * @return string
	 */
	public function generatePw($pw, $salt) {
		return md5 ( md5 ( $pw . $salt ) );
	}
	
	/**
	 * 获取一个随机生成的字符串
	 *
	 * @param unknown_type $username        	
	 */
	protected function _getSalt($str = "") {
		return substr ( md5 ( $str . time () ), 2, 16 );
	}
	/**
	 * 修改资料
	 * @authorjing
	 */
	public function SavePersonDetail ($userID, $name, $mail, $qq , $sentence) {
		$name = !empty($name) ? "`name`='" . $name . "'" : "`name`=NULL"  ;
		$mail = !empty($mail) ? "`mail`='" . $mail . "'" : "`mail`=NULL"  ;
		$qq = !empty($qq) ? "`qq`='" . $qq . "'" : "`qq`=NULL"  ;
		$sentence = !empty($sentence) ? "`sentence`='" . $sentence . "'" : "`sentence`=NULL"  ;
		$sql = "UPDATE $this->_table
				SET $name,
					$mail,$qq,$sentence,`create_time` = NOW() WHERE `id` =" . $userID;
		return $this->update ( $sql );
	}
	/**
	 * 修改头像
	 * @authorjing
	 */
	public function Updateicon ($userID, $icon) {
		$sql = "UPDATE $this->_table
				SET `icon` = '".$icon."' WHERE `id` =" . $userID;
		return $this->update ( $sql );
	}
	
}